package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author xunyuhua
 * @since 2022-06-26
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Autowired
    private IApUserService apUserService;


    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getStatus() != null) {
            queryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }

        Page<ApUserRealname> page1 = this.page(page, queryWrapper);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), page1.getTotal(), page1.getRecords());
        return result;

    }

    /**
     * 实名认证
     *
     * @param dto
     * @param type 0：实名认证驳回；   1：实名认证通过
     * @return
     */
    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int type) {
        ApUserRealname userRealname = this.getById(dto.getId());

        if (type == 0) {
            userRealname.setStatus(2);
            userRealname.setReason(dto.getMsg());
            this.updateById(userRealname);
        } else {
            ApUser apUser = apUserService.getById(userRealname.getUserId());
            //远程调用自媒体(media)微服务，创建自媒体人wm_user数据
            WmUser wmUser = saveWmUser(apUser);
            //远程调用文章(article)微服务，创建文章作者ap_author数据
            ApAuthor apAuthor = saveAuthor(apUser,wmUser);

            //生成了文章作者，wmuser中就可以给ApAuthorId赋值
            wmUser.setApAuthorId(apAuthor.getId());
            mediaFeign.updateWmUser(wmUser);

            //修改app用户状态
            //修改userRealname状态
            userRealname.setStatus(9);
            this.updateById(userRealname);
            //修改apuser状态
            apUser.setIsIdentityAuthentication(true);
            apUser.setFlag(1);
            apUserService.updateById(apUser);


        }
        return ResponseResult.okResult();

    }

    @Autowired
    private ArticleFeign articleFeign;

    private ApAuthor saveAuthor(ApUser apUser,WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
//        apAuthor.setId();
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);
        apAuthor.setUserId(apUser.getId());
//        apAuthor.setCreatedTime();
        apAuthor.setWmUserId(wmUser.getId());

        ResponseResult<ApAuthor> responseResult = articleFeign.saveApAuthor(apAuthor);
        if (responseResult.getCode() == 0) {
            return responseResult.getData();
        } else {
            throw new RuntimeException("远程调用失败");
        }

    }

    @Autowired
    private MediaFeign mediaFeign;

    private WmUser saveWmUser(ApUser apUser) {
        WmUser wmUser = new WmUser();
//        wmUser.setId();
        wmUser.setApUserId(apUser.getId());
//        wmUser.setApAuthorId();
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
//        wmUser.setNickname();
        wmUser.setImage(apUser.getImage());
//        wmUser.setLocation();
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(9);
//        wmUser.setEmail();
        wmUser.setType(0);
        wmUser.setScore(0);
//        wmUser.setLoginTime();
//        wmUser.setCreatedTime();

        ResponseResult<WmUser> responseResult = mediaFeign.saveWmUser(wmUser);
        if (responseResult.getCode() == 0) {
            return responseResult.getData();
        } else {
            throw new RuntimeException("远程调用失败");
        }
    }

}
